package com.fattiger.framework.sessionshare.session;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

public abstract class AbstractSessionManager implements SessionManager {

    protected String getSessionIdFromCookie(HttpServletRequest request){
        Cookie[] cookies= request.getCookies();
        if(cookies != null){
	        for(Cookie cookie:cookies){
	            if(cookie.getName().equals(SESSION_COOKIE_KEY)){
	                return cookie.getValue();
	            }
	        }
        }
        return null;
    }

    protected String generateSessionId(){
        return UUID.randomUUID().toString();
    }

    protected int getSessionExpire(HttpServletRequest request){
        Object sessionExpire = request.getServletContext().getInitParameter(SESSION_EXPIRE_KEY);
        if(sessionExpire == null){
            return DEFAULT_SESSION_EXPIRE;
        }
        return Integer.parseInt(sessionExpire.toString().trim());
    }
}
